package com.bigdata.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bigdata.common.utils.StringUtils;
import com.bigdata.common.core.page.TableDataInfo;
import com.bigdata.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.bigdata.service.domain.bo.UserPointsBo;
import com.bigdata.service.domain.vo.UserPointsVo;
import com.bigdata.service.domain.UserPoints;
import com.bigdata.service.mapper.UserPointsMapper;
import com.bigdata.service.service.IUserPointsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户积分账户Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-15
 */
@RequiredArgsConstructor
@Service
public class UserPointsServiceImpl implements IUserPointsService {

    private final UserPointsMapper baseMapper;

    /**
     * 查询用户积分账户
     */
    @Override
    public UserPointsVo queryById(Long userId){
        return baseMapper.selectVoById(userId);
    }

    /**
     * 查询用户积分账户列表
     */
    @Override
    public TableDataInfo<UserPointsVo> queryPageList(UserPointsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserPoints> lqw = buildQueryWrapper(bo);
        Page<UserPointsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户积分账户列表
     */
    @Override
    public TableDataInfo<UserPointsVo> selectUserPageList(UserPointsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserPoints> lqw = buildQueryWrapper(bo);
        Page<UserPointsVo> result = baseMapper.selectUserPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户积分账户列表
     */
    @Override
    public List<UserPointsVo> queryList(UserPointsBo bo) {
        LambdaQueryWrapper<UserPoints> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserPoints> buildQueryWrapper(UserPointsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserPoints> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPoints() != null, UserPoints::getTotalPoints, bo.getTotalPoints());
        lqw.eq(bo.getCumulativePoints() != null, UserPoints::getCumulativePoints, bo.getCumulativePoints());
        lqw.like(StringUtils.isNotBlank(bo.getCreateBy()), UserPoints::getCreateBy, bo.getCreateBy());
        return lqw;
    }

    /**
     * 新增用户积分账户
     */
    @Override
    public Boolean insertByBo(UserPointsBo bo) {
        UserPoints add = BeanUtil.toBean(bo, UserPoints.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改用户积分账户
     */
    @Override
    public Boolean updateByBo(UserPointsBo bo) {
        UserPoints update = BeanUtil.toBean(bo, UserPoints.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserPoints entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户积分账户
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
